package com.z.system.dto;

import java.text.SimpleDateFormat;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * 用户数据传输对象，用于将用户实体转换为前端所需的格式
 */
public class UserDTO {
    // 前端期望字段
    private String id; // 对应前端 id 字段
    private String userName; // 对应前端 userName 字段
    private String userSn;
    private String userEmail; // 对应前端 userEmail 字段
    private String nickName; // 对应前端 nickName 字段
    private String userPhone; // 对应前端 userPhone 字段
    private String userGender; // 对应前端 userGender 字段（格式化后的性别）
    private String status; // 对应前端 status 字段（格式化后的状态）
    private String remark;
    private String headImgUrl;
    private String createTime;
    private String updateTime;
    private String deptId;
    private String deptName;
    private Set<String> roleIds;

    // 日期格式化器
    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * 构造函数，将User实体转换为DTO，与前端字段对应
     */
    public UserDTO(com.z.system.model.User user, Set<UUID> roleIds) {
        this.id = user.getId() != null ? user.getId().toString() : null;
        this.userName = user.getUsername();
        this.userSn = user.getUserSn();
        this.userEmail = user.getEmail();
        this.nickName = user.getNickname();
        this.userPhone = user.getPhone();
        // 格式化性别：1=男，0=女
        this.userGender = user.getSex() != null ? (user.getSex() == 1 ? "男" : "女") : null;
        // 格式化状态：启用=1，禁用=2
        this.status = user.getEnable() != null ? (user.getEnable() == 1 ? "1" : "2") : "2";
        this.remark = user.getRemark();
        this.headImgUrl = user.getHeadImgUrl();
        this.createTime = user.getCreateTime() != null ? DATE_FORMAT.format(user.getCreateTime()) : null;
        this.updateTime = user.getUpdateTime() != null ? DATE_FORMAT.format(user.getUpdateTime()) : null;
        this.deptId = user.getDeptId() != null ? user.getDeptId().toString() : null;
        this.deptName = user.getDeptName(); // 现在User实体通过连表查询已经包含了deptName字段
        this.roleIds = roleIds != null ? roleIds.stream().map(UUID::toString).collect(Collectors.toSet()) : null;
    }

    public UserDTO() {
    }

    // Getters and Setters
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserSn() {
        return userSn;
    }

    public void setUserSn(String userSn) {
        this.userSn = userSn;
    }

    public String getUserEmail() {
        return userEmail;
    }

    public void setUserEmail(String userEmail) {
        this.userEmail = userEmail;
    }

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    public String getUserPhone() {
        return userPhone;
    }

    public void setUserPhone(String userPhone) {
        this.userPhone = userPhone;
    }

    public String getUserGender() {
        return userGender;
    }

    public void setUserGender(String userGender) {
        this.userGender = userGender;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public String getHeadImgUrl() {
        return headImgUrl;
    }

    public void setHeadImgUrl(String headImgUrl) {
        this.headImgUrl = headImgUrl;
    }

    public String getCreateTime() {
        return createTime;
    }

    public void setCreateTime(String createTime) {
        this.createTime = createTime;
    }

    public String getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(String updateTime) {
        this.updateTime = updateTime;
    }

    public String getDeptId() {
        return deptId;
    }

    public void setDeptId(String deptId) {
        this.deptId = deptId;
    }

    public String getDeptName() {
        return deptName;
    }

    public void setDeptName(String deptName) {
        this.deptName = deptName;
    }

    public Set<String> getRoleIds() {
        return roleIds;
    }

    public void setRoleIds(Set<String> roleIds) {
        this.roleIds = roleIds;
    }
}